package logic

import (
	"context"
	"fmt"
	"hotel/model/mysql"
	"hotel/model/redis"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HotelLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHotelLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HotelLoginLogic {
	return &HotelLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HotelLoginLogic) HotelLogin(in *hotelrpc.HotelLoginRequest) (*hotelrpc.HotelLoginResponse, error) {
	// todo: add your logic here and delete this line

	get := redis.Client.Get("sendSmslogin" + in.Tel)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("短信验证码不正确，请重新输入")
	}
	var findhotel mysql.Hotel
	mysql.DB.Debug().Where("username=?", in.Username).Find(&findhotel)
	if findhotel.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}
	if findhotel.Password != in.Password {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}
	mysql.DB.Debug().Find(&findhotel)
	redis.Client.Del("sendSmslogin" + in.Tel)

	return &hotelrpc.HotelLoginResponse{Id: int64(findhotel.ID)}, nil
}
